package com.java.util.servlet;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.alibaba.fastjson2.JSONObject;
import com.java.entity.RaceAccount;
import com.java.entity.RaceAdmin;
import com.java.util.MyCons;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;

public class ServletUtil {

	public static ServletRequestAttributes getRequestAttributes() {
		return (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
	}

	public static HttpServletRequest getRequest() {
		return getRequestAttributes().getRequest();
	}

	public static HttpSession getSession() {
		return getRequest().getSession();
	}

	public static String getUserId(String userId) {
		if (!MyTools.isEmpty(userId)) {
			if ("not".equals(userId)) {
				return null;
			}
			return userId;
		}
		if (adminIsLogin()) {
			RaceAdmin acc = (RaceAdmin) getRequest().getSession().getAttribute(MyCons.ADMIN_ROLE);
			return acc.getAdminId();
		}
		if (userIsLogin()) {
			RaceAccount acc = (RaceAccount) getRequest().getSession().getAttribute(MyCons.USER_ROLE);
			return acc.getAccountId();
		}
		return userId;
	}

	public static boolean isAjax(HttpServletRequest request) {
		boolean isAsyncStarted = request.isAsyncStarted();
		return isAsyncStarted;
	}

	public static boolean adminIsLogin() {
		Object user = getSession().getAttribute(MyCons.ADMIN_ROLE);
		if (user == null) {
			return false;
		}
		return true;
	}

	public static boolean userIsLogin() {
		Object user = getSession().getAttribute(MyCons.USER_ROLE);
		if (user == null) {
			return false;
		}
		return true;
	}

	public static boolean isStudent() {
		if (userIsLogin() == false) {
			return false;
		}
		Object type = getSession().getAttribute(MyCons.USER_ROLE_TYPE);
		if ("0".equals(type.toString())) {
			return true;
		}
		return false;
	}

	public static boolean isTeacher() {
		if (userIsLogin() == false) {
			return false;
		}
		Object type = getSession().getAttribute(MyCons.USER_ROLE_TYPE);
		if ("1".equals(type.toString())) {
			return true;
		}
		return false;
	}

	public static boolean isGroup() {
		if (userIsLogin() == false) {
			return false;
		}
		Object type = getSession().getAttribute(MyCons.USER_ROLE_TYPE);
		if ("2".equals(type.toString())) {
			return true;
		}
		return false;
	}

	public static void noLogin(HttpServletResponse response) throws Exception {
		response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);
		response.setCharacterEncoding("UTF-8");
		response.setContentType("application/json; charset=utf-8");
		BaseRespone res = new BaseRespone(HttpServletResponse.SC_UNAUTHORIZED, "未登录");
		PrintWriter out = null;
		out = response.getWriter();
		out.write(JSONObject.toJSONString(res));
		out.flush();
		out.close();
	}
}
